"""
@author: zrp
@file: 2023-12-04-ATM功能作业.py
@time: 2023/12/4 17:23
"""
import json
import os.path


# 用户信息推荐用{"username":{"passwd":"123456","count":1000}}


# 存钱
def save(count, count1):
    count += count1
    return count


# 取钱
def take(count, count1):
    if count - count1 >= 0:
        count -= count1
        return count


# 转账
def transfer(user, dst_user, count, number):
    result = user[count] - number
    # print(result<0)
    if result<0:
        return "",""
    else:
        user[count] -= number
        dst_user[count] += number
        return user, dst_user


# 登录
def login(user_info, name, passwd1, passwd):
    if name in user_info and user_info[name][passwd1] == passwd:
        return True
    return False
#注册
def regist(user_info,name, passwd, count=0):
    if name in user_info:
        name = {"passwd":passwd,"count":count}
    return name


# 主函数
path = "./data"
if not os.path.isdir(path):
    os.mkdir(path)
filename = f"{path}/user_info.txt"
# print(filename)
with open(filename, "r") as fp:
    fp.seek(0)
    text1 = fp.read()
    user_info = json.loads(text1)

while True:
    print("欢迎使用ATM系统，\n1.登录\n2.注册\n3.退出\n")
    num = input("请输入您想使用的功能:")
    if num == "1":
        while True:
            name = input("请输入您的用户名:")
            passwd = input("请输入您的密码:")
            passwd1 = "passwd"
            if login(user_info, name, passwd1, passwd):
                name1 = user_info[name]
                count = user_info[name]["count"]
                print(f"{name}您好，欢迎登录ATM系统，您的余额为{count}")
                print("a.存钱\nb.取钱\nc.转账\nd.退出\n")
                str1 = input("请选择您想要使用的功能:")
                if str1 == "a":
                    while True:
                        try:
                            num2 = int(input("请输入您想要存入的金额:"))
                            user_info[name] = save(count, num2)
                            # print(user_info[name]["count"])
                            with open(filename, "w") as f:
                                user_info1 = json.dumps(user_info)
                                f.write(user_info1)
                            print(f"业务办理成功,已存入{num2}元")
                            break
                        except:
                            print("输入的不是数字")

                elif str1 == "b":
                    while True:
                        try:
                            num2 = int(input("请输入您想要取出的金额:"))
                            if take(count, num2):
                                user_info[name]["count"] = take(count, num2)
                                with open(filename, "w") as f:
                                    user_info1 = json.dumps(user_info)
                                    f.write(user_info1)
                                print("业务办理成功！")
                                break
                            else:
                                print("余额不足，请重新输入")
                        except:
                            print("输入的不是数字")

                elif str1 == "c":

                    try:
                        num2 = int(input("请输入您想要转出的金额:"))
                        recv = input("请输入收款人:")
                        if recv not in user_info:
                            print("用户不存在")
                        else:
                            count1 = "count"
                            recv1 = user_info[recv]
                            user_info[name], user_info[recv] = transfer(name1, recv1, count1, num2)
                            if user_info[name] and user_info[recv]:
                                with open(filename, "w") as f:
                                    user_info1 = json.dumps(user_info)
                                    f.write(user_info1)
                                print("业务办理成功！")
                            else:
                                print("余额不足，请重新输入")
                    except:
                            print("输入的不是数字")

                elif str1 == "d":
                    print("退出成功")
                    break
                else:
                    print("输入不合法")
            else:
                print("用户名或密码错误")

    elif num == "2":
        while True:
            name = input("请输入您的用户名:")
            passwd = input("请输入您的密码:")
            if  regist(user_info,name, passwd):
                user_info[name] = regist(user_info,name, passwd)
                with open(filename, "w") as f:
                    user_info1 = json.dumps(user_info)
                    f.write(user_info1)
                print(f"用户{name}注册成功！")
                break


    elif num == "3":
        print("成功退出！")
        break
    else:
        print("输入不合法，请重新输入！")
